import sys
import json
import smtplib
from email.mime.text import MIMEText
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("email")
@mcp.tool()
def send_email(to, subject, body):
    msg = MIMEText(body, 'plain', 'utf-8')
    msg['From'] = 'huanxi919@qq.com'
    msg['To'] = to
    msg['Subject'] = subject

    try:
        smtp = smtplib.SMTP_SSL('smtp.qq.com', 465)  # 使用SSL连接
        smtp.login('huanxi919@qq.com', 'ducaqjhcjdwscgaa')
        smtp.sendmail('huanxi919@qq.com', to, msg.as_string())
        smtp.quit()
        return True
    except Exception as e:
        print(f"发送邮件时出错: {str(e)}", file=sys.stderr)
        return False

def main():
    # 测试数据
    test_data = {
        "arguments": {
            "to": "xx@qq.com",
            "subject": "Test Email",
            "body": "This is a test email"
        }
    }
    
    try:
        success = send_email(
            test_data['arguments']['to'], 
            test_data['arguments']['subject'], 
            test_data['arguments']['body']
        )
        result = {"status": "success" if success else "error"}
    except Exception as e:
        result = {"status": "error", "message": str(e)}
    
    print(json.dumps(result))
    sys.stdout.flush()

# 启动MCP服务器
if __name__ == "__main__":
    mcp.run()